<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_Web extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
	}
	function _format_appendSql()
	{
		if (isset($_SESSION['acct']))
		{
			if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
			{
				//为管理
				//$append['']
				$append['1'] = 1;
	
			}else
			{
				//$append['admin_id'] = $_SESSION['acct']['admin_id'];
				$append['agent_id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
				$append['agent_id'][] = $_SESSION['acct']['id'];
	
			}
		}
		return $append;
	}
	function actioncaller()
	{
		if ($this->_context->isget())
		{}else
		{
			$bindname = $this->_context->bindname;
		    
			//$pwd = $this->_context->pwd;
			//$user = Userbindtel::bindtelToUserObj($bindname);
			//if(!$user->id())
		//	{
		//		return $this->error('不存在绑定号码');
		//	}else if ($user->password !== $pwd){
		//		return $this->error('密码输入错误');
		//	}
			$destination_number = $this->_context->destination_number;
		$rnd  = $this->_context->rndcode;
			
			if (!Helper_Ckcode::check($rnd)){
				return $this->success('验证码错误');
				exit();
			}
			$isone = Cbsubmit::find('bindname = ? and destination_number = ?',$bindname,$destination_number)->getCount('id');
			if($isone)
			{
			//	return $this->error('对不起,已经存在此次号码呼叫,请勿重复提交');
			}
			Cbsubmit::call($bindname, $destination_number);
			return $this->success('提交呼叫任务成功');
		}
	}
	function actiongroupcaller()
	{
		if ($this->_context->isget())
		{
		}else
		{
			$bindname = $this->_context->bindname;
			//$pwd = $this->_context->pwd;
			//$user = Userbindtel::bindtelToUserObj($bindname);
			//if(!$user->id())
			//	{
			//		return $this->error('不存在绑定号码');
			//	}else if ($user->password !== $pwd){
			//		return $this->error('密码输入错误');
			//	}
			$destination_number = $this->_context->destination_number;
			$password = $this->_context->password;
			if (!$destination_number)
			{
				return $this->error('会议号码有误');
			}else{
				$destination_number = explode(',', $destination_number);
				
				foreach ($destination_number as $k=>$v)
				{
					
					if(!is_numeric(($v)))
					{
						return $this->error('会议号码格式错误');
						break;
					}
				}
			}
			$isone = conference::find('caller = ? and called = ?',$bindname,implode('-',$destination_number))->getCount('id');
	    	if($isone)
			{
				return $this->error('对不起,已经存在此次号码呼叫,请勿重复提交');
			}
			Cbsubmit::groupcall($bindname, implode('-', $destination_number),$password);
			return $this->success('提交呼叫任务成功');
		}
	}
	function actionweb800set()
	{
		$id = 0;
		
		if($_SESSION['acct']['acclevel'] != 3)
		{
			$id = intval($this->_context->id);
			#代理
			if ($id)
			{
				$param = array_merge(array('id'=>$id),$this->_format_appendSql());
				$acct = Acctuser::find($param)->getOne();
			}else{
				exit('err');
			}
			//$acct = Acct::find('id = ? and acctname = ?',$_SESSION['acct']['id'],$_SESSION['acct']['acctname'])->getone();
			
		}else{
			$acct = Acctuser::find('id = ? and acctname = ?',$_SESSION['acct']['id'],$_SESSION['acct']['acctname'])->getone();
		}
		
		$acct->createDefaultweb800();
		if($this->_context->isPOST())
		{
			$work_status = intval($this->_context->work_status);
			$work_title = trim($this->_context->work_title);
			$work_start = trim($this->_context->work_start);
			$work_end = trim($this->_context->work_end);
			
			if(!$acct->web800->id)
			{
				$union = new Web800();
				$union->agent_id = $acct->id;
				$union->work_start = $work_start;
				$union->work_end = $work_end;
				$union->work_title = $work_title;
				$union->work_status = $work_status;
				$union->save(0,'create');
			}else{
				$union = $acct->web800;
				$union->work_start = $work_start;
				$union->work_end = $work_end;
				$union->work_title = $work_title;
				$union->work_status = $work_status;
				$union->save(0,'update');
			}
			return $this->success('设置成功');
		}else{
			
			$this->_view['acct'] = $acct;
		}
		
	}
	function _loadtoken()
	{
		$id = $_SESSION['acct']['agent_id'];
		if ($_SESSION['acct']['acclevel'] == 0 )
		{
			$token = Acct::find('id = ?',$_SESSION['acct']['id'])->setColumns('id,tbtoken')->getOne();
		}else{
			$token = acct::find('id = ?',intval($id))->setColumns('id,tbtoken')->getOne();
		}
		if (!$token->id())
		{
			return '';
		}else{
			return trim($token->tbtoken);
		}
		//$taobao = Taobaotoken::find()->getOne();
		
	}
	function actiontaobao88()
	{
		$result = '';
		$session = $this->_loadtoken();
		if($session == '')
		{
			return $this->error('上级未设置TOKEN');
		}
		if(isset($_REQUEST['orderid']) && isset($_REQUEST['acctname']))
		{
			$rnd  = $this->_context->post('rndcode');
			if (!Helper_Ckcode::check($rnd)){
				return $this->error('验证码错误');
			}
			require_once '../lib/top/TopClient.php';
			require_once '../lib/top/request/TradeFullinfoGetRequest.php';
			require_once '../lib/top/request/LogisticsDummySendRequest.php';
			require_once  '../lib/top/RequestCheckUtil.php';
			$orderid = trim($this->_context->orderid);
			$acctname = trim($this->_context->acctname);
			$querypwd = md5(time().$acctname.$orderid);
			$bind = trim($this->_context->bind);
			$acct = Acct::find('acctname = ?',$acctname)->getOne();
			if(!$acct->id() )
			{
				$result = '代理不存在';
				return $this->error($result);
			}
			if($acct->taobao_flag == 1)
			{
				$result = '已经设置后台自动充值,不能手动操作,,如需要取消或者更换淘宝账号请先关闭自动充值再重新绑定!';
				return $this->error($result);
			}
			$c = new TopClient;
			$c->appkey = '21612259';
			$c->secretKey = '39a31b83172056116d82ac8af7ad7a4b';
				
			$req = new TradeFullinfoGetRequest;
			$req->setFields("status,num,total_fee,price,buyer_nick");
			$req->setTid($orderid);
			$querytaobao = $c->execute($req, $session);
			#$resp->trade->num
			if($querytaobao->trade->status == 'WAIT_SELLER_SEND_GOODS')
			{
				$req = new LogisticsDummySendRequest();
				$req->setTid($orderid);
				$resp = $c->execute($req,$session);
				if($resp->shipping->is_success){
					Helper_Common::log("TAOBAO SEND OK,ORDERDATA=".print_r($querytaobao,true),'taobao_web');
					$cardinfo = '';
					for ($i=1;$i<=$querytaobao->trade->num;$i++)
					{
					$card = array(
							'cardname'=>$orderid.'_'.$i,
							'password'=>$querypwd,
							'balance' =>$querytaobao->trade->price,
							'voucher_acctname'=>'',
							'vouchered_acctname'=>'',
							'vouchereddate'=>'0000-00-00',
							'agent_id'=> $_SESSION['acct']['agent_id'] == 1 ? -1 : $_SESSION['acct']['agent_id'] ,
							'create_acctname'=>'taobao'
					);
							//$cardinfo .= sprintf("%s,%s<br>",$orderid.'_'.$i,$querypwd);
					Acct::make_agentcard($card);
					$status = Acct::use_agentcard($acct->id, $card['cardname'], $card['password']);
					if ($status === true)
					{
					$acct_name = $acct->acctname;
					$log = new Syslog();
					$log->acctname = $acct_name;
					$log->admin_id = 1;
					$log->agent_id = $acct->id;
					$log->clientip = $_SERVER['REMOTE_ADDR'];
					$log->logtext = "使用淘宝代理充值卡{$card['cardname']} 为 {$acct_name} 充值{$card['balance']}成功";
					$log->save(0,'create');
						
					}
					}
					#绑定淘宝
					if($bind)
					{
					$acct->taobao_flag = 1;
					$acct->buyer_nick = $querytaobao->trade->buyer_nick;
						$acct->save(0,'update');
					}
					$result = '充值成功,可在充值记录查询,并且请在淘宝确认收货!';
					return $this->success($result);
				}
		
				}else{
				$result = '订单状态错误';
				return $this->error($result);
				}
		
				}
				//$this->_view['result'] = $result;
	}
	
	function actiontaobao11()
	{
		$result = '';
		$session = $this->_loadtoken();
		if($session == '')
		{
			return $this->error('上级未设置TOKEN');
		}
		if(isset($_REQUEST['orderid']) && isset($_REQUEST['acctname']))
		{
			$rnd  = $this->_context->post('rndcode');
			if (!Helper_Ckcode::check($rnd)){
				$result='验证码错误!';
				return $this->error($result);
				// 				return $this->_redirectMessage('失败', '验证码错误', ('/interface/taobao11'));
				// 				exit;
			}
			require_once '../lib/top/TopClient.php';
			require_once '../lib/top/request/TradeFullinfoGetRequest.php';
			require_once '../lib/top/request/LogisticsDummySendRequest.php';
			require_once  '../lib/top/RequestCheckUtil.php';
			$orderid = trim($this->_context->orderid);
			$acctname = trim($this->_context->acctname);
			$querypwd = md5(time().$acctname.$orderid);
			try {
		
			} catch (Exception $e) {
			}
			$c = new TopClient;
			$c->appkey = '21612259';
			$c->secretKey = '39a31b83172056116d82ac8af7ad7a4b';
		
			$req = new TradeFullinfoGetRequest;
			$req->setFields("status,num,total_fee,price");
			$req->setTid($orderid);
			$querytaobao = $c->execute($req, $session);
			#$resp->trade->num
			if($querytaobao->trade->status == 'WAIT_SELLER_SEND_GOODS')
			{
				$card = Taobaocard::find("used = 0 and type = '11' and price = ? ",$querytaobao->trade->price)->limit(0,$querytaobao->trade->num)->query();
				if(count($card) != $querytaobao->trade->num)
				{
					$result='充值卡池余卡不够,请联系管理员!';
					return $this->error($result);
				}
				$req = new LogisticsDummySendRequest();
				$req->setTid($orderid);
				$resp = $c->execute($req,$session);
				if($resp->shipping->is_success){
					Helper_Common::log("TAOBAO SEND OK,ORDERDATA=".print_r($querytaobao,true),'taobao_web');
		
					#验证成功
						
					Helper_Common::log("DATA LOG:".print_r($card,true));
					foreach ($card as $i=>$v)
					{
					$v->used = 1;
						$v->orderid = $orderid;
						$v->acctname = $acctname;
						$v->querypwd = $querypwd;
						$v->usetime = date('Y-m-d H:i:s');
						$v->save();
						$result .= sprintf("%s,%s<br>",$v->cardname,$v->cardpwd);
					}
					$result = "验证成功,$orderid 订单产生如下卡号,卡密:<br>".$result;
					$result =  $result."<br>请牢记账号和订单号,方便查询记录,请复制方便以后查询使用.";
					//exit
					return $this->success($result);
				}
		
				}else{
				$result="订单状态错误";
				return $this->error($result);
		}
		
		}
			//return $this->success($result);
	}
}


